/**
  **************************************************************************
  * @file     at32f421_int.c
  * @brief    main interrupt service routines.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */
extern void transfercomplete(void);
extern void PeriodElapsedCallback(void);
extern void interruptRoutine(void);
extern void tenKhzRoutine(void);
extern void sendDshotDma(void);
extern void receiveDshotDma(void);


extern char send_telemetry;
extern char telemetry_done;
extern char servoPwm;

/* includes ------------------------------------------------------------------*/
#include "at32f421_int.h"
#include "main.h"
#include "targets.h"
#include "adc.h"
#include "IO.h"
/** @addtogroup AT32F421_periph_template
  * @{
  */

/** @addtogroup 421_LED_toggle
  * @{
  */

/**
  * @brief  this function handles nmi exception.
  * @param  none
  * @retval none
  */
void NMI_Handler(void)
{
}

/**
  * @brief  this function handles hard fault exception.
  * @param  none
  * @retval none
  */
void HardFault_Handler(void)
{
  /* go to infinite loop when hard fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles memory manage exception.
  * @param  none
  * @retval none
  */
void MemManage_Handler(void)
{
  /* go to infinite loop when memory manage exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles bus fault exception.
  * @param  none
  * @retval none
  */
void BusFault_Handler(void)
{
  /* go to infinite loop when bus fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles usage fault exception.
  * @param  none
  * @retval none
  */
void UsageFault_Handler(void)
{
  /* go to infinite loop when usage fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles svcall exception.
  * @param  none
  * @retval none
  */
void SVC_Handler(void)
{
}

/**
  * @brief  this function handles debug monitor exception.
  * @param  none
  * @retval none
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  this function handles pendsv_handler exception.
  * @param  none
  * @retval none
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  this function handles systick handler.
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
}

void DMA1_Channel1_IRQHandler(void){
    if(dma_flag_get(DMA1_FDT1_FLAG) == SET)		
    {
        DMA1->clr = DMA1_GL1_FLAG;
#ifdef USE_ADC
        ADC_DMA_Callback();
#endif			
        if(dma_flag_get(DMA1_DTERR1_FLAG) == SET)
        {
            DMA1->clr = DMA1_GL1_FLAG;
        }
    }
}

void DMA1_Channel3_2_IRQHandler(void)
{
    if(dma_flag_get(DMA1_FDT2_FLAG) == SET)
    {
        DMA1->clr = DMA1_GL2_FLAG;
        DMA1_CHANNEL2->ctrl_bit.chen = FALSE;   
    }
    if(dma_flag_get(DMA1_DTERR2_FLAG) == SET)
    {
        DMA1->clr = DMA1_GL2_FLAG;
        DMA1_CHANNEL2->ctrl_bit.chen = FALSE;
    }
}

void DMA1_Channel5_4_IRQHandler(void)
{
	
	
#ifdef USE_TIMER_15_CHANNEL_1
    if(dma_flag_get(DMA1_HDT5_FLAG) == SET){
        if(servoPwm){
            IC_TIMER_REGISTER->cctrl_bit.c1p = TMR_INPUT_FALLING_EDGE;	
            DMA1->clr = DMA1_HDT5_FLAG;
        }
    }

    if(dma_flag_get(DMA1_FDT5_FLAG) == SET)		
    {
        DMA1->clr = DMA1_GL5_FLAG;
        INPUT_DMA_CHANNEL->ctrl_bit.chen = FALSE;
        transfercomplete();
        TMR14->cval = TMR14->pr -2;
    }
    if(dma_flag_get(DMA1_DTERR5_FLAG) == SET)
    {
        DMA1->clr = DMA1_GL5_FLAG;
    }
#endif
#ifdef USE_TIMER_3_CHANNEL_1
    if(dma_flag_get(DMA1_HDT4_FLAG) == SET){ 
        if(servoPwm){
            IC_TIMER_REGISTER->cctrl_bit.c1p = TMR_INPUT_FALLING_EDGE;
            DMA1->clr = DMA1_HDT4_FLAG;
        }
    }

    if(dma_flag_get(DMA1_FDT4_FLAG) == SET)		
    {
        DMA1->clr = DMA1_GL4_FLAG;
        INPUT_DMA_CHANNEL->ctrl_bit.chen = FALSE;
        transfercomplete();
        TMR14->cval = TMR14->pr -2;
    }
    if(dma_flag_get(DMA1_DTERR4_FLAG) == SET)
    {
        DMA1->clr = DMA1_GL4_FLAG;
    }
#endif
			
}

/**
  * @brief This function handles ADC and COMP interrupts (COMP interrupts through EXTI lines 21 and 22).
  */
void ADC1_CMP_IRQHandler(void)
{
    if((EXINT->intsts & EXTI_LINE) != (uint32_t)RESET)
    {
        EXINT->intsts = EXTI_LINE;
        interruptRoutine();
    }
}

/**
  * @brief This function handles TIM6 global and DAC underrun error interrupts.
  */
void TMR14_GLOBAL_IRQHandler(void)
{
    TMR14->ists = (uint16_t)~TMR_OVF_FLAG;
    tenKhzRoutine();
}

/**
  * @brief This function handles TIM14 global interrupt.
  */
void TMR16_GLOBAL_IRQHandler(void)
{
    TMR16->ists = 0x00;
    PeriodElapsedCallback();
}

void TMR15_GLOBAL_IRQHandler(void)
{
    TMR15->ists = (uint16_t)~TMR_OVF_FLAG;
    TMR15->iden &= ~TMR_OVF_INT;
    //	sendDshotDma();
    //	IC_TIMER_REGISTER->ctrl1_bit.tmren = TRUE;
}



/**
  * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25.
  */
void USART1_IRQHandler(void)
{

}

void TMR3_GLOBAL_IRQHandler(void)
{
    //	COM_TIMER->iden &= ~TMR_OVF_INT; // disable interrupt    
    TMR3->ists = (uint16_t)~TMR_C1_FLAG;
    TMR3->ists = (uint16_t)~TMR_OVF_FLAG; 
    //
}



void EXTI4_15_IRQHandler(void){

}


/******************************************************************************/
/*                 AT32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_at32f413_xx.s).                                            */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */
